package com.ay.selfservicemachine.dialog;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.ay.selfservicemachine.R;
import com.ay.selfservicemachine.base.HQWAppCompatActivity;
import com.ay.selfservicemachine.bean.UpdateBean;
import com.ay.selfservicemachine.databinding.DialogUpdateBinding;
import com.ay.selfservicemachine.http.download.DownloadListener;
import com.ay.selfservicemachine.http.download.HQWDownloadManager;
import com.ay.selfservicemachine.http.download.HttpMachine;
import com.ay.selfservicemachine.permission.PermissionCommander;
import com.ay.selfservicemachine.permission.PermissionListener;
import com.ay.selfservicemachine.utils.AppUtils;
import com.ay.selfservicemachine.utils.HQWPermissionUtil;
import com.ay.selfservicemachine.utils.ToastUtil;


import java.io.File;

public class UpdateDialog extends BaseDialog<DialogUpdateBinding> {

    HttpMachine httpMachine = new HttpMachine();
    UpdateBean updateBean;
    boolean isDownloading = false;//是否真正下载
    boolean isDownload = false;//是否已经下载
    HQWAppCompatActivity hqwActivity;

    public void setUpdateBean(UpdateBean updateBean) {
        this.updateBean = updateBean;
        root.tvVersionName.setText("v" + updateBean.getUpdateVersion().getVersion());
        root.tvRemark.setText(updateBean.getUpdateVersion().getRemark());
        root.btnClose.setVisibility(updateBean.getUpdateVersion().getIsMust() == 1 ? View.GONE : View.VISIBLE);
    }

    public UpdateDialog(@NonNull Context context, HQWAppCompatActivity hqwActivity) {
        super(context);
        this.hqwActivity=hqwActivity;
    }

    @Override
    protected void initDialog() {
        super.initDialog();
        setBorderExtension();
        setGravity(Gravity.CENTER);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected DialogUpdateBinding viewBinding() {
        return DialogUpdateBinding.inflate(getLayoutInflater());
    }


    @Override
    protected void initView() {
        super.initView();
        root.btnClose.setOnClickListener((v) -> {
            httpMachine.stop();
            dismiss();
        });
        root.btnUpdate.setOnClickListener((v) -> {
            downloadApk();
        });
    }


    public void downloadApk(){
        File file = new File( hqwActivity.getExternalCacheDir()+"/ticket.apk");;
        if (isDownload) {
            installApk(file);
            return;
        }
        if (isDownloading) {
            ToastUtil.toastLongMessage(getContext().getString(R.string.updating));
            return;
        }
        isDownloading = true;
        root.btnUpdate.setText("0%");
        root.pbUpdateApp.setProgress(0);
        root.btnUpdate.setTextColor(getContext().getColor(R.color.black));

        HQWDownloadManager.DownloadFileSharpPullback(updateBean.getUpdateVersion().getDownloadUrl(), file, httpMachine, new DownloadListener() {
            @Override
            public void onDownloading(File file, long progress, long max, String HQWprogress, String HQWmax) {
                root.pbUpdateApp.post(() -> {
                    root.pbUpdateApp.setProgress((int) Math.ceil(Float.valueOf(progress) / Float.valueOf(max) * 100));
                    root.btnUpdate.setText((int) Math.ceil(Float.valueOf(progress) / Float.valueOf(max) * 100) + "%");
                    if ((int) Math.ceil(Float.valueOf(progress) / Float.valueOf(max) * 100) > 50) {
                        root.btnUpdate.setTextColor(getContext().getColor(R.color.white));
                    } else {
                        root.btnUpdate.setTextColor(getContext().getColor(R.color.black));
                    }
                });

            }

            @Override
            public void onSucceed(File file) {
                root.pbUpdateApp.post(() -> {
                    isDownload = true;
                    root.pbUpdateApp.setProgress(100);
                    root.btnUpdate.setText(getContext().getString(R.string.install));
                   installApk(file);

                });

            }

            @Override
            public void onStop(File file) {

            }

            @Override
            public void onFailed(File file, Exception e) {
                root.pbUpdateApp.post(() -> {
                    isDownloading = false;
                    root.pbUpdateApp.setProgress(100);
                    root.btnUpdate.setText(getContext().getString(R.string.update));
                    ToastUtil.toastShortMessage(getContext().getString(R.string.update_failure)+"\n"+e.getMessage());
                });

            }
        });
    }


    public void installApk(File file){
        PermissionCommander.startSpecialVerify(
                hqwActivity,
                HQWPermissionUtil.SYSTEM_APK_INSTALL,
                false,
                getContext().getString(R.string.install_apk_permission),"",
                new PermissionListener() {
                    @Override
                    public void onsucceed() {
                        AppUtils.InstallApk(getContext(), file);
                    }

                    @Override
                    public void onfailure() {
                        ToastUtil.toastShortMessage( getContext().getString(R.string.install_apk_permission));
                        hqwActivity.finish();
                    }
                });
    }




}
